package com.powerbank.usercenter.controller;

import com.powerbank.common.result.Result;
import com.powerbank.usercenter.dto.SmsCodeDTO;
import com.powerbank.usercenter.dto.UserLoginDTO;
import com.powerbank.usercenter.dto.UserRegisterDTO;
import com.powerbank.usercenter.service.AuthService;
import com.powerbank.usercenter.service.SmsService;
import com.powerbank.usercenter.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 用户认证控制器
 */
@Tag(name = "用户认证", description = "用户认证相关接口")
@Slf4j
@RestController
@RequestMapping("/auth")
@Validated
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private SmsService smsService;

    /**
     * 发送短信验证码
     */
    @Operation(summary = "发送短信验证码", description = "发送登录或注册短信验证码")
    @PostMapping("/sms/send")
    public Result<Void> sendSmsCode(@Valid @RequestBody SmsCodeDTO smsCodeDTO) {
        log.info("发送短信验证码: phone={}, type={}", smsCodeDTO.getPhone(), smsCodeDTO.getType());
        smsService.sendSmsCode(smsCodeDTO.getPhone(), smsCodeDTO.getType());
        return Result.success();
    }

    /**
     * 短信验证码登录
     */
    @Operation(summary = "短信验证码登录", description = "使用手机号和短信验证码登录")
    @PostMapping("/login/sms")
    public Result<UserLoginVO> loginBySms(@Valid @RequestBody UserLoginDTO loginDTO) {
        log.info("短信验证码登录: phone={}", loginDTO.getPhone());
        UserLoginVO loginVO = authService.loginBySms(loginDTO);
        return Result.success(loginVO);
    }

    /**
     * 账号密码登录
     */
    @Operation(summary = "账号密码登录", description = "使用手机号和密码登录")
    @PostMapping("/login/password")
    public Result<UserLoginVO> loginByPassword(@Valid @RequestBody UserLoginDTO loginDTO) {
        log.info("账号密码登录: phone={}", loginDTO.getPhone());
        UserLoginVO loginVO = authService.loginByPassword(loginDTO);
        return Result.success(loginVO);
    }

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "新用户注册")
    @PostMapping("/register")
    public Result<UserLoginVO> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        log.info("用户注册: phone={}", registerDTO.getPhone());
        UserLoginVO loginVO = authService.register(registerDTO);
        return Result.success(loginVO);
    }

    /**
     * 刷新Token
     */
    @Operation(summary = "刷新Token", description = "使用RefreshToken刷新AccessToken")
    @PostMapping("/refresh")
    public Result<UserLoginVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        log.info("刷新Token");
        UserLoginVO loginVO = authService.refreshToken(refreshToken);
        return Result.success(loginVO);
    }

    /**
     * 验证Token
     */
    @Operation(summary = "验证Token", description = "验证Token有效性")
    @PostMapping("/verify")
    public Result<Boolean> verifyToken(@RequestHeader("Authorization") String token) {
        log.info("验证Token");
        boolean isValid = authService.verifyToken(token);
        return Result.success(isValid);
    }

    /**
     * 忘记密码
     */
    @Operation(summary = "忘记密码", description = "通过短信验证码重置密码")
    @PostMapping("/password/reset")
    public Result<Void> resetPassword(@RequestParam("phone") String phone,
                                     @RequestParam("smsCode") String smsCode,
                                     @RequestParam("newPassword") String newPassword) {
        log.info("重置密码: phone={}", phone);
        authService.resetPassword(phone, smsCode, newPassword);
        return Result.success();
    }
}